#ifndef __GAME_UTIL_H__
#define __GAME_UTIL_H__

#include "cocos2d.h"
#include "Setting.h"
#include "EnemyManager.h"
#include "BulletManager.h"
#include "ToothManager.h"
#include "WaveManager.h"
#include "Player.h"
#include "GameStateLayer.h"
#include "GameUpgradeScene.h"
#include "Shield.h"


using namespace cocos2d;

class GameUtil
{
	CC_SYNTHESIZE(Player*, d_player, Player)
	CC_SYNTHESIZE(EnemyManager*, d_enemyManager, EnemyManager)
	CC_SYNTHESIZE(BulletManager*, d_bulletManager, BulletManager)
	CC_SYNTHESIZE(ToothManager*, d_toothManager, ToothManager)
	CC_SYNTHESIZE(WaveManager*, d_waveManager, WaveManager)
	CC_SYNTHESIZE(GameStateLayer*, d_stateLayer, GameStateLayer)
	CC_SYNTHESIZE(CCScene*, d_upgradeScene, UpgradeScene)
	CC_SYNTHESIZE(Setting*, d_setting, Setting)
	CC_SYNTHESIZE(CCScene*, d_scene, GameScene)
	CC_SYNTHESIZE(CCLayer*, d_gameLayer, GameLayer)
	CC_SYNTHESIZE(Shield*, d_shield, Shield)


public:
	~GameUtil(void);
	GameUtil(CCScene* scene);
	static GameUtil* instance();

	static int random(int max) {
		max++;
		return (rand() % max);
	}

	static int random(int min, int max){
		max++;
		return (rand() % (max- min) + min);
	}

	static int random(int min, int max, int step){
		max++;
		float value = random(min, max) - min;
		// round (+ 0.5f)
		int ret = int(value/step + 0.5f) * step + min;
		if(ret > max) {
			ret = max;
		}

		return ret;
	}

	static void pause(CCLayer * node) {  
		node->setTouchEnabled(false);
		pause((CCNode*)node); 

	} 
	
	static void pause(CCNode * node) {  
		node->pauseSchedulerAndActions();  

		CCObject * obj;  
		CCARRAY_FOREACH(node -> getChildren(), obj) {  
			CCNode * n = (CCNode *)obj;  
			pause(n);   
		}  
	} 
	
	static void resume(CCLayer * node) {  
		node->setTouchEnabled(true); 
		resume((CCNode*)node); 

	} 

	static void resume(CCNode * node) {  
		node->resumeSchedulerAndActions();  

		CCObject * obj;  
		CCARRAY_FOREACH(node -> getChildren(), obj) {  
			CCNode * n = (CCNode *)obj;  
			resume(n);   
		}  
	} 

	static float getCreatureY(float y) { 
		int height = 120;
		return (20 + height/2 + (5-y)*height);
	}  
	
	static CCRect getRect(float percent, CCSprite* sprite);
	void gameover();
	void waveEnded();
	void pause();
	void updateVolume();

private:
	static GameUtil* d_ins;
};

#endif // __GAME_UTIL_H__

